package com.bupt.ilink.controller.bailian;

import com.aliyun.bailian20231229.Client;
import com.aliyun.bailian20231229.models.*;
import com.bupt.ilink.config.BailianConfig;
import com.bupt.ilink.service.bailian.FileService;
import com.bupt.ilink.tool.BailianFileTools;
import com.bupt.ilink.utils.R;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.bupt.ilink.service.bailian.impl.*;


import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@RequestMapping("/bailian")
public class FileController {
    @Autowired
    FileService fileService;

    @PostMapping("/upload/files")
    public R upload(@RequestParam("files") List<MultipartFile> files, String path) throws Exception {
         return fileService.uploadFiles(files, path);
    }

    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file, String path,@RequestParam(required = false,defaultValue = "") String newFilename) throws Exception {
        return fileService.uploadFile(file, path,newFilename);
    }




    @PostMapping("/create/category")
    public R createCategory(@RequestBody String jsonString) throws Exception {
        JSONObject jsonObject = new JSONObject(jsonString);
        String path = jsonObject.getString("path");
        String directory = jsonObject.getString("directory");
        return fileService.createCategory(path, directory);
    }

    @PostMapping("/delete/file")
    public R deleteFile(String path, String fileName) throws Exception { // TODO:这里的fileName也要加后缀
        return fileService.deleteFile(path, fileName);
    }

    @PostMapping("/delete/category")
    public R deleteCategory(String path) throws Exception {
        return fileService.deleteCategory(path);
    }
}
